﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ImageResizer;

namespace Utils
{
    public class CroppingModel
    {
        public int X1; //top-left point of an rectangle
        public int Y1; //top-left point of an rectangle
        public int X2; //bottom-right point of an rectangle
        public int Y2; //bottom-right point of an rectangle
    }

    public class CroppingHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="srcWidth">source width</param>
        /// <param name="srcHeight">source height</param>
        /// <param name="destWidth">the dest width (just for ratio here)</param>
        /// <param name="destHeight">the dest height (just for ratio here)</param>
        /// <returns></returns>
        public static CroppingModel GetCroppingModel(int srcWidth, int srcHeight, int destWidth, int destHeight)
        {
            int width = 0;
            int height = 0;
            double ratio = 0.0;

            if (destWidth < destHeight)
            {
                ratio = (double)destHeight / (double)destWidth;
                if (srcWidth * ratio <= srcHeight)
                {
                    width = srcWidth;
                    height = Convert.ToInt32(srcWidth * ratio);
                }
                else
                {
                    width = Convert.ToInt32(srcHeight / ratio);
                    height = srcHeight;
                }
            }
            else
            {
                ratio = (double)destWidth / (double)destHeight;
                if (srcHeight * ratio <= srcWidth)
                {
                    height = srcHeight;
                    width = Convert.ToInt32(srcHeight * ratio);
                }
                else
                {
                    width = srcWidth;
                    height = Convert.ToInt32(srcWidth / ratio);
                }
            }

            CroppingModel info = new CroppingModel();
            if (width == srcWidth)
            {
                info.X1 = 0;
                info.Y1 = Convert.ToInt32((srcHeight - height) / 2.0);
                info.X2 = srcWidth;
                info.Y2 = info.Y1 + height;
            }
            else
            {
                info.X1 = Convert.ToInt32((srcWidth - width) / 2.0);
                info.Y1 = 0;
                info.X2 = info.X1 + width;
                info.Y2 = srcHeight;
            }
            return info;

        }

        /// <summary>
        /// Quality < 30, we will use default value
        /// </summary>
        /// <param name="source">image source</param>
        /// <param name="dest">dest file name, not extension e.g. /files/9/1/2001/mytest</param>
        /// <param name="destWidth">dest width</param>
        /// <param name="destHeight">dest height</param>
        /// <returns>return the cropped file name, including extension e.g. /files/9/1/2001/mytest.jpg</returns>
        public static string Build(Image source, string dest, int destWidth, int destHeight, int quality = -1)
        {
            if (source == null)
            {
                throw new Exception("the image source can't be null");
            }
            int width = source.Width;
            int height = source.Height;
            CroppingModel info = GetCroppingModel(width, height, destWidth, destHeight);

            string cropSetting = "width=" + destWidth + "&height=" + destHeight + "&crop=(" + info.X1 + "," + info.Y1 + "," + info.X2 + "," + info.Y2 + ")&quality=" + quality;
            if (quality < 30)
            {
                cropSetting = "width=" + destWidth + "&height=" + destHeight + "&crop=(" + info.X1 + "," + info.Y1 + "," + info.X2 + "," + info.Y2 + ")";
            }
            string thumbFile = ImageBuilder.Current.Build(source, dest, new ResizeSettings(cropSetting), false, false);
            return thumbFile;
        }
    }
}
